package online.longzipeng.cinemasys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.config.RedisConfig;
import online.longzipeng.cinemasys.dto.session.SessionQueryResponseDTO;
import online.longzipeng.cinemasys.dto.session.SessionWebQueryResponse;
import online.longzipeng.cinemasys.entity.Session;
import online.longzipeng.cinemasys.service.SessionService;
import online.longzipeng.cinemasys.vo.session.SessionQueryVO;
import online.longzipeng.cinemasys.vo.session.SessionSaveVO;
import online.longzipeng.cinemasys.vo.session.SessionUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/session")
@Api(value = "场次相关接口",description = "场次相关接口",tags = "场次相关接口")
public class SessionController {

    @Autowired
    SessionService sessionService;
    @Autowired
    RedisTemplate redisTemplate;


    @PostMapping(value = "/get-list-page")
    @ApiOperation("查询所有场次带分页")
    public Result getListPage(@RequestBody SessionQueryVO queryVO){
        IPage<SessionQueryResponseDTO> typeIPage = sessionService.getListPage(queryVO);
        return Result.success(typeIPage);
    }

    @PostMapping(value = "/get-list")
    @ApiOperation("查询所有场次不分页")
    public Result getList(@RequestBody SessionQueryVO queryVO){
        SessionWebQueryResponse result = sessionService.getList(queryVO);
        return Result.success(result);
    }

    @GetMapping("/get-by-id")
    @ApiOperation("通过id查询场次详情")
    public Result getById(@RequestParam(value = "id",required = true)Long id){
        return Result.success(sessionService.findById(id));
    }


    @PostMapping("/save")
    @NeedPermissions("session:save")
    @ApiOperation("新增场次")
    public Result save(@Valid @RequestBody SessionSaveVO sessionSaveVO){
        Session session = new Session();
        BeanUtils.copyProperties(sessionSaveVO,session);
        sessionService.add(session);
        return Result.success(null,"场次添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("session:delete")
    @ApiOperation("删除场次")
    public Result deleteOne(@ApiParam(value = "场次id",required = true)Long id){
        try {
            sessionService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"场次删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("session:delete")
    @ApiOperation("删除多个场次")
    public Result deleteOne(@ApiParam(value = "场次id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            sessionService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"场次删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("session:update")
    @ApiOperation("修改场次")
    public Result update(@Valid @RequestBody SessionUpdateVO sessionUpdateVO) {
        Session session = new Session();
        BeanUtils.copyProperties(sessionUpdateVO,session);
        sessionService.updateSession(session);
        return Result.success(null,"场次修改成功！！");
    }

    @PostMapping("/get-seats-by-session-id")
    public Result getSeatsBySessionId(Long id){
        return Result.success(redisTemplate.opsForValue().get(RedisConfig.SESSION_KEY+"id"));
    }
}
